// DrawRectCtrler.cpp: implementation of the CDrawRectCtrler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "../Resource.h"
#include "DrawRectCtrler.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDrawRectCtrler::CDrawRectCtrler(COLORREF color, int width, long style, BOOL fill)
: m_lStartx(0), m_lStarty(0), m_lEndx(0),  m_lEndy(0)
{
	SetColor (color);		// set the pen color
	SetPenWidth (width);	// set the pen width
	SetPenStyle (style);	// set the pen style
	SetFill (fill);			// set fill or not
	m_TaskTyle = ID_RECT;	// a rect
}

CDrawRectCtrler::~CDrawRectCtrler()
{
}

void CDrawRectCtrler::DrawObject(CDC* pDC)
{
	//
	// Set the CPen //////////////////////////////////////////
	//
	CPen pen(GetPenStyle(), GetPenWidth(), GetColor());
	int oldmode = pDC->GetROP2();
	CPen* oldpen = pDC->SelectObject(&pen);

	//
	// Set the CBrush /////////////////////////////////////////
	//
	CBrush* oldbrush;
	CBrush brush(GetColor());
	if( !GetFill() )
	{
		// set the brush as null brush ( not filling )
		oldbrush = (CBrush*)pDC->SelectStockObject(NULL_BRUSH);
	}
	else
	{
		// Set the brush to fill the image.
		oldbrush = (CBrush*)pDC->SelectObject(brush);
	}

	// recorde the draw rect with logical coordination :
	m_DrawRect.left = min(m_lStartx, m_lEndx) - GetPenWidth();
	m_DrawRect.right = max(m_lStartx, m_lEndx) + GetPenWidth();
	m_DrawRect.top = min(m_lStarty, m_lEndy) - GetPenWidth();
	m_DrawRect.bottom = max(m_lStarty, m_lEndy) + GetPenWidth();

	// complete drawing the rect.
	pDC->Rectangle(m_lStartx, m_lStarty, m_lEndx, m_lEndy);

	// recover the DC.
	pDC->SetROP2(oldmode);
	pDC->SelectObject(oldpen);
	pDC->SelectObject(&oldbrush);
	pen.DeleteObject();
}

void CDrawRectCtrler::StartPoint (long x, long y)
{
	m_lStartx = m_lEndx = x;
	m_lStarty = m_lEndy = y;
}

void CDrawRectCtrler::MoveAt (CDC *pDC, long x, long y)
{
	//
	// Set the CPen //////////////////////////////////////////
	//
	COLORREF XorColor = pDC->GetBkColor() ^ GetColor();
	CPen pen (GetPenStyle(), GetPenWidth(), XorColor);		// create a pen
	int oldmode = pDC->GetROP2();
	CPen* oldpen = pDC->SelectObject(&pen);

	//
	// Set the CBrush /////////////////////////////////////////
	//
	CBrush *oldbrush;
	CBrush brush(XorColor);
	if( !GetFill() )
	{
		// set the brush as null brush ( not filling )
		oldbrush = (CBrush*)pDC->SelectStockObject(NULL_BRUSH);
	}
	else
	{
		// Set the brush to fill the image.
		oldbrush = (CBrush*)pDC->SelectObject(brush);
	}

	// Set the Raster Operation To R2_XORPEN (dest = src XOR dest)
	pDC->SetROP2(R2_XORPEN);

	//
	// Draw the rect in MoveAt()
	//

	pDC->Rectangle(m_lStartx, m_lStarty, m_lEndx, m_lEndy);	
		// delete the original image

	//
	// Reset the coordinaton.
	//
	m_lEndx = x;
	m_lEndy = y;

	// using the new coordination to redraw the image.
	pDC->Rectangle(m_lStartx, m_lStarty, m_lEndx, m_lEndy);

	// recover the DC :
	pDC->SetROP2(oldmode);
	pDC->SelectObject(oldpen);
	pDC->SelectObject(&oldbrush);
	pen.DeleteObject();
}